﻿using Google.Protobuf.Compiler;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MVVMDemo
{

    public partial class Form1 : Form
    {
        public CustomerModelViewModel vmCustomer;

        private CustomerModel customerModel;
        
        public Form1()
        {
            InitializeComponent();

            vmCustomer = new CustomerModelViewModel();
            vmCustomer.CustomerModelBindingSource = customerModelBindingSource;



            this.Load += delegate { vmCustomer.Load(); };
            btnNew.Click += delegate { vmCustomer.New(null); };
            btnDelete.Click += delegate { vmCustomer.Delete(customerModel); };
            btnSave.Click += delegate { vmCustomer.Save(); };


            btnSetID.Click += delegate { vmCustomer.SetID(); };

            
            this.FormClosing += delegate { vmCustomer.Dispose(); };

        }


        private void btnSetID_Click(object sender, EventArgs e)
        {

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            // 新增: 传的是一个空model，并且把viewModel的当前CurrentCustomerModel设为modelToEdit            
            CustomerModel modelToEdit = new CustomerModel();
            vmCustomer.CurrentCustomerModel = modelToEdit;

            // Form 窗口， 把viewModel传给Form，而没有传model给Form？？？
            // 传model也是可以的
            //var editModelForm = new UserEditFrm(vmCustomer);
            var editModelForm = new UserEditFrm(modelToEdit);

            if (editModelForm.ShowDialog() == DialogResult.OK)
            {
                MessageBox.Show(vmCustomer.CurrentCustomerModel.CustomerName);

                vmCustomer.New(modelToEdit);

                // 定位滚动到新增行
                int rowIndex = dataGridView1.RowCount - 1;
                DisplayNewRow(dataGridView1, rowIndex);
            }

        }

        private CustomerModel GetCurrentModel()
        {
            //// 获取当前选中的行
            //var currentRow = dataGridView1.CurrentRow;

            //// 获取模型数据，假设第一列存储着模型的唯一标识（如 ID）
            //var modelId = Convert.ToInt32(currentRow.Cells["ID"].Value); // 假设 ID 列名为 "ID"

            //// 根据 ID 从数据库或业务逻辑层获取对应的 Model 对象
            //var model = YourBusinessLayer.GetModelById(modelId);


            // 获取当前选中的行
            var currentRow = this.dataGridView1.CurrentRow;

            // 直接获取行对应的 Model 对象
            var model = (CustomerModel)currentRow.DataBoundItem;

            return model;

        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            var modelToEdit = GetCurrentModel(); // Get the person instance to edit

            vmCustomer.CurrentCustomerModel = modelToEdit;

            // Form 窗口
            //var editModelForm = new UserEditFrm(vmCustomer);
            var editModelForm = new UserEditFrm(modelToEdit);

            if (editModelForm.ShowDialog() == DialogResult.OK)
            {
                var editeModel = vmCustomer.CurrentCustomerModel;

                //MessageBox.Show(editeModel.CustomerName);
                //vmCustomer.New(modelToEdit);
            }

        }



        private void DisplayNewRow(DataGridView dgv, int rowIndex)
        {
            bool isDislaying = rowIndex > dgv.FirstDisplayedScrollingRowIndex && rowIndex < dgv.FirstDisplayedScrollingRowIndex + dgv.DisplayedRowCount(true) - 1;
            if (!isDislaying)
            {
                dgv.FirstDisplayedScrollingRowIndex = rowIndex;
                dgv.CurrentCell = dataGridView1[0, rowIndex];  // 0 = 第1列
            }
        }
    }
}
